import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Player')
export class Player extends Component {
	@property(Node)
	public body: Node = null;

	private _shotDuration: number = 30;
	private _shotTimer: number = 0;

	start() {
		this.node.on(Node.EventType.TOUCH_MOVE, this.ontTouchMove, this);
	}

	onDestroy() {
		this.node.off(Node.EventType.TOUCH_MOVE, this.ontTouchMove, this);
	}

	update(deltaTime: number) {
		this._shotTimer += deltaTime;
		if (this._shotTimer >= this._shotDuration) {
			this._shotTimer = 0;
			this.onShot();
		}
	}

	onShot() {
		console.log('shot');
	}

	ontTouchMove(event: TouchEvent) {
		const { pageX, pageY } = event.touches[0];
		this.node.setPosition(pageX, pageY);
	}

	init() {}
}
